<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 上传用户自定义头像类
 */
class Upload{
        private $IC;                                            //CI对象
	public $upload_target_dir;				//文件被上传到的目标目录
	//构造函数
	public function __construct()
	{
                $this->CI = &get_instance();
		$this->upload_target_dir= BASEPATH . '../data/upfiles/pic/'.date('Ymd');
	}
	// 处理头像图片开始--->
	public function upload_file($where=array())
	{
            $result = array();
            $result['success'] = false;
            $result['code'] = 0;
            $result['msg'] = '';
            $result['data'] = array();
            $avatar_number = 0;
            $msg = '';
            //头像图片(file 域的名称：__avatar1,2,3...)。
            $avatars = array("__avatar1", "__avatar2");
            $avatars_length = count($avatars);
            for ( $i = 0; $i < $avatars_length; $i++ )
            { 
                $size = $i == 0 ? 'b' : 's';
                $avatar = $_FILES[$avatars[$i]];
                $avatar_number = $i + 1;
                if ( $avatar['error'] > 0 )
                {
                    $msg .= $avatar['error'];
                }else{
                    is_dir($this->upload_target_dir) or (mkdir($this->upload_target_dir, 0777));
                    $pic_name = (10000000000+time()+$where['user_id'])."_".$size.'.jpg';//图片名称
                    $savePath = $this->upload_target_dir."/".$pic_name;//图片保存路径
                    if(move_uploaded_file($avatar["tmp_name"], $savePath)){
                        $success_num++;
                        array_push($result['data'],'/data/upfiles/pic/'.date("Ymd").'/'.$pic_name);
                    }
                    
                }
                
            } 
            $result['msg'] = $msg;
            if($success_num > 0){
                $result['success'] = true;
            }
            
            return $result;
	}
   /**
    *获取文件扩展名
    *@param String $filename 要获取文件名的文件
    */
   public function getFileExt($filename){
   		$info = pathinfo($filename);
   		return $info["extension"];
   }
	
}
